import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useTokenStore } from '@/stores/useTokenStore'
import { ElMention, ElMessage } from 'element-plus'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      children: [
        {
          path: '/ingredients',
          name: 'ingredients',
          component: () => import('../views/ingredients/index.vue')
        },
        {
          path: '/dishes',
          name: 'dishes',
          component: () => import('../views/dishes/index.vue')
        },
        {
          path: '/prepare',
          name: 'prepare',
          component: () => import('../views/prepare/index.vue')
        },
        {
          path: '/order',
          name: 'order',
          component: () => import('../views/order/index.vue')
        },
        {
          path: '/chil',
          name: 'chil',
          component: () => import('@/views/children.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue')
    }
  ]
})

/* 全局前置路由守卫 */
router.beforeEach((to, from) => {
  const tokenStore = useTokenStore()
  const { path, fullPath } = to
  if (path == '/login') {
    if (tokenStore.getToken) {
      ElMessage.success('不需要再次登录')
      return { path: '/' }
    }
  } else {
    if (!tokenStore.getToken) {
    
      ElMessage.warning('您还未登录,请登录后操作')
      return { path: '/login', query: { destPath: fullPath } }
    }
  }
})


export default router
